import com.xiaoleilu.hutool.http.HttpUtil
import com.xiaoleilu.hutool.io.file.FileWriter
import org.jsoup.Jsoup
import java.io.File
import java.util.regex.Pattern

/**
 * Created by chenlia on 2017/11/10.
 */
class Utils {
    companion object{
        var pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
        var exf = File("D:\\9yin\\exception.txt")
        var exfw = java.io.FileWriter(exf,true)

        fun getArticle( url:String ):Unit{
            try{
                var str = HttpUtil.get(url);

                var element = Jsoup.parse(str).getElementsByClass("mainCont")[0]
                var title = try{element.getElementsByClass("modTitLink").text()}catch (e:Exception){"异常"}
                var titleTime = try{element.getElementsByClass("newsContTime").text()}catch (e:Exception){"异常"}
                var content = try{element.getElementsByClass("newsContText").html()}catch (e:Exception){"异常"}

                title= pattern.matcher(title).replaceAll(" ");
                println("title = $title , content = \r\n $content ")
                File("D:\\9yin\\${titleTime}_${title}.html").writeText(content)
            }catch (e:Exception){
                e.printStackTrace()
                exfw.append(url + "\r\n" + e.localizedMessage + "\r\n\r\n")
            }
            exfw.flush()
            exfw.close()
        }
        fun getArticlePath( str:String ):List<String>{
            var doc = Jsoup.parse(str)
            var elements = doc.getElementsByTag("li")
            println("elements.size = ${elements.size}")
            var list:MutableList<String> = mutableListOf()
            for ( element in elements ){
                var s = element.getElementsByTag("a")[0].attr("href")
                list.add(s)
            }
            return list;
        }
    }
}

val artileUrlList = mutableListOf<String>()
val path = "http://9yin.woniu.com/news/sysnotice/list_%s.html"
fun main(args: Array<String>) {
    var docStr = HttpUtil.get("http://9yin.woniu.com/news/sysnotice/index.html");
    artileUrlList.addAll(Utils.getArticlePath(docStr))

    for ( i in 2..88 ){//88
        var pathTemp = String.format(path,i)
        var docTemp = HttpUtil.get(pathTemp)
        var listTemp = Utils.getArticlePath(docTemp)
        artileUrlList.addAll(listTemp)
        println("获取第 $i 页完成,目录：$listTemp ")
    }

    var f = File("D:\\9yin\\index.txt")
    var fw = java.io.FileWriter(f,true)
    for ( url in artileUrlList ){
        fw.append(url+"\r\n")
    }
    fw.flush()
    fw.close()
    println("目录获取完成...")

    for ( url in artileUrlList ){
        Utils.getArticle(url)
    }
}